package com.ymt.bpm.web.action;

import com.ymt.bpm.dao.MDao;
import com.ymt.bpm.service.engine.task.PiService;
import com.ymt.bpm.service.engine.task.TiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * Created by Johnny on 2017/4/18.
 */
@Controller
public class IndexController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Environment env;

    @Autowired
    private MDao dao;

    @Autowired
    private PiService ps;

    @Autowired
    private TiService ts;

    @RequestMapping(path="/login")
    public ModelAndView login(HttpServletRequest request) {
        String[] profile = env.getActiveProfiles();
        String productionVersion = env.getProperty("bpmp.production-version", "0");
        String cdn = env.getProperty("bpmp.cdn", "/bpmp-resources");

        //get redirect url
        String successUrl = "/";
        //DefaultSavedRequest dsr = (DefaultSavedRequest)request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
        String sessionSccuessUrl = (String)request.getSession().getAttribute("successUrl");
        /*if (dsr!=null) {
            successUrl = dsr.getRedirectUrl();
            if (log.isTraceEnabled()) {
                log.trace("getRequestURI="+dsr.getRequestURI());
                log.trace("getRequestURL="+dsr.getRequestURL());
                log.trace("getRedirectUrl="+dsr.getRedirectUrl());
                log.trace("successUrl="+successUrl);
            }
        } else */if (sessionSccuessUrl!=null) {
            successUrl = sessionSccuessUrl;
            log.trace("successUrl="+successUrl);
        }

        /*Enumeration enu = request.getHeaderNames();
        while (enu.hasMoreElements()) {
            log.debug("======"+enu.nextElement());
        }*/

        ModelAndView view = new ModelAndView("login");
        view.addObject("profile", profile[0]);
        view.addObject("productionVersion", productionVersion);
        view.addObject("cdn", cdn);
        view.addObject("authtype", env.getProperty("bpmp.auth-type"));
        view.addObject("successUrl", successUrl);
        return view;
    }

    /*@RequestMapping(path="/openapi/tologin", produces = MediaType.APPLICATION_JSON_VALUE)
    public void toLogin(HttpServletRequest request, HttpServletResponse resp) {

        String path = request.getRequestURI();
        if (log.isDebugEnabled()) {
            log.debug("toLogin while requesting: "+path);
        }

        Enumeration enu = request.getHeaderNames();
        while (enu.hasMoreElements()) {
            log.debug("======"+enu.nextElement());
        }
        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies) {
            log.debug("===="+c.getName()+":"+c.getValue()+":"+c.getPath()+" : "+c.getDomain());
        }

        resp.setContentType(MediaType.APPLICATION_JSON_VALUE);
        try {
            resp.getOutputStream().print("{\"error\":\"META_DESC_LOGIN\"}");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/

}
